package jsen.tool.ddd.demo.domain.parking;

import jsen.tool.ddd.adapter.infrastructure.specification.ISpecification;
import jsen.tool.ddd.demo.domain.base.DomainObject;
import jsen.tool.ddd.demo.domain.parking.spec.loc.LocationSpec;
import jsen.tool.ddd.domain.ValueObject;
import lombok.Builder;
import lombok.Getter;

/**
 * @author jsen
 * @date 2021/10/6
 */
@ValueObject(name = "Location", desc = "车位，职责负责车位状态维护")
@Getter
@Builder
public class Location implements DomainObject {
    private final ISpecification<Location> strict = new LocationSpec();

    private final int x;
    private final int y;
    private final int w;
    private final int h;

    public boolean isMixWith(Location loc) {
        int xL = loc.x - loc.w / 2;
        int xR = loc.x + loc.w / 2;
        int yL = loc.y - loc.h / 2;
        int yH = loc.y + loc.h / 2;
        return isIn(xL, yL) || isIn(xL, yH) || isIn(xR, yL) || isIn(xR, yH);
    }

    private boolean isIn(int x, int y) {
        int xL = x - w / 2;
        int xR = x + w / 2;
        int yL = y - h / 2;
        int yH = y + h / 2;
        return x >= xL && x <= xR && y >= yL && y <= yH;
    }
}
